package com.tanhua.server.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.model.vo.UserInfoVoAge;
import com.tanhua.server.service.SettingsService;
import com.tanhua.server.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 通用设置管理 控制层
 */
@RestController
@RequestMapping("/users")
public class SettingsController {

    @Autowired
    private SettingsService settingsService;


    /**
     * 用户通用设置 - 读取
     */
    @RequestMapping(value = "/settings", method = RequestMethod.GET)
    public ResponseEntity settings() {
        SettingsVo settingsVo = settingsService.settings();
        return ResponseEntity.ok(settingsVo);
    }


    /**
     * 设置陌生人问题 - 保存
     */
    @RequestMapping(value = "/questions", method = RequestMethod.POST)
    public ResponseEntity saveQuestions(@RequestBody Map<String, String> params) {
        String content = params.get("content");//陌生人问题
        settingsService.saveQuestions(content);
        return ResponseEntity.ok(null);
    }


    /**
     * 通知设置 - 保存
     */
    @RequestMapping(value = "/notifications/setting", method = RequestMethod.POST)
    public ResponseEntity saveSettings(@RequestBody SettingsVo settingsVo) {
        settingsService.saveSettings(settingsVo);
        return ResponseEntity.ok(null);
    }


    /**
     * 黑名单分页列表查询
     */
    @RequestMapping(value = "/blacklist", method = RequestMethod.GET)
    public ResponseEntity blacklistPage(@RequestParam(value = "page", defaultValue = "1") Long page, @RequestParam(value = "pagesize", defaultValue = "10") Long pagesize) {
        // 限制pagesize的最大值，防止查100W条记录
        pagesize = pagesize>50?50:pagesize;
        PageResult<UserInfoVoAge> pageResult = settingsService.blacklistPage(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }


    /**
     * 黑名单 - 移除
     */
    @RequestMapping(value = "/blacklist/{uid}", method = RequestMethod.DELETE)
    public ResponseEntity removeBlackList(@PathVariable("uid") Long blackUserId){
        settingsService.removeBlackList(blackUserId);
        return ResponseEntity.ok(null);
    }

}
